/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__filter_init.c                                   */
/*  NOTE      =                                                    */
/*  DATE      = 2014/12/12 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>
#include <protobuf-c/protobuf-c.h>

#include "../../comm/frcmd_parse_detail.h"
#include "../../comm/client.pb-c.h"
#include "../../comm/frcmd_parse.h"
#include "../../comm/apm.pb-c.h"
#include "../../comm/frcmd_client.h"
#include "../../comm/frcomm.h"
#include "../../comm/frmem.h"
#include "../../comm/frsutl.h"
#include "../../comm/apmtest_cunit.h"

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

int __filter_init(Command *p_st_command, FilterCommand **pp_st_filter_cmd, Filter **pp_filter);

void case1(void)
{
    int i_ret = 0;
    Command *p_st_cmd = NULL;
    FilterCommand *p_filter_cmd = NULL;
    FilterCommand *p_filter_cmd_tmp = NULL;
    Filter *p_filters = NULL;
    Filter *p_filters_tmp = NULL;
    int i_size_tmp = 0;

    //设置filters
    p_filters_tmp = frmalloc(sizeof(Filter));
    filter__init(p_filters_tmp);
    p_filters_tmp->ip_proto = 1;
    i_size_tmp = strlen("192.168.1.1") + 1;
    p_filters_tmp->ip_from = frmalloc(i_size_tmp);
    snprintf(p_filters_tmp->ip_from,i_size_tmp,"%s","192.168.1.1");

    i_size_tmp = strlen("192.168.1.2") + 1;
    p_filters_tmp->ip_to = frmalloc(i_size_tmp);
    snprintf(p_filters_tmp->ip_to,i_size_tmp,"%s","192.168.1.2");

    i_size_tmp = strlen("22") + 1;
    p_filters_tmp->port_from = frmalloc(i_size_tmp);
    snprintf(p_filters_tmp->port_from,i_size_tmp,"%s","22");

    i_size_tmp = strlen("6356") + 1;
    p_filters_tmp->port_to = frmalloc(i_size_tmp);
    snprintf(p_filters_tmp->port_to,i_size_tmp,"%s","6536");

    i_size_tmp = strlen("60:00:ef:00:ea:56") + 1;
    p_filters_tmp->mac = frmalloc(i_size_tmp);
    snprintf(p_filters_tmp->mac,i_size_tmp,"%s","60:00:ef:00:ea:56");

    //设置filter_cmd
    p_filter_cmd_tmp = frmalloc(sizeof(FilterCommand));
    filter_command__init(p_filter_cmd_tmp);
    p_filter_cmd_tmp->action = frmalloc(strlen("add") + 1);
    snprintf(p_filter_cmd_tmp->action,strlen("add") + 1,"%s","add");
    p_filter_cmd_tmp->n_filters = 1;
    p_filter_cmd_tmp->filters = &p_filters_tmp;

    //设置p_st_cmd
    p_st_cmd = frmalloc(sizeof(Command));
    command__init(p_st_cmd);
    p_st_cmd->filter_cmd = p_filter_cmd_tmp;
    p_st_cmd->cmd_type = 2;

    i_ret = __filter_init(p_st_cmd, &p_filter_cmd, &p_filters);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    CU_ASSERT_PTR_NULL(p_filter_cmd->action);
    CU_ASSERT_PTR_NULL(p_filters->ip_to);
    CU_ASSERT_PTR_NULL(p_filters->port_from);
    CU_ASSERT_PTR_NULL(p_filters->port_to);
    CU_ASSERT_PTR_NULL(p_filters->mac);
    CU_ASSERT_EQUAL(p_filters->ip_proto,0);
    CU_ASSERT_EQUAL(p_st_cmd->cmd_type,COMMAND__COMMAND_TYPE__Type_FilterCommand);
    CU_ASSERT_EQUAL(p_filter_cmd->n_filters,0);

    frfree(p_filters_tmp->ip_from);
    frfree(p_filters_tmp->ip_to);
    frfree(p_filters_tmp->port_from);
    frfree(p_filters_tmp->port_to);
    frfree(p_filters_tmp->mac);
    frfree(p_filters_tmp);
    frfree(p_filters->ip_from);
    frfree(p_filters->ip_to);
    frfree(p_filters->port_from);
    frfree(p_filters->port_to);
    frfree(p_filters->mac);
    frfree(p_filters);
    frfree(p_filter_cmd_tmp->action);
    frfree(p_filter_cmd_tmp);
    frfree(p_filter_cmd->action);
    frfree(p_filter_cmd);
    frfree(p_st_cmd);


}


int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t__filter_init 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);

    return 0;

}

int main(int argc, char **argv)
{
    frmem_init();
    run_cunit(add_suite);
    frshow_leak();
    frmem_destroy();
    return 0;
}